<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image src="../../static/img/logo.png"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<view class="main-list oBorder">
					<input class="main-input" v-model="phoneData" type="text" maxlength="11" placeholder="请输入手机号" />
				</view>
				<view class="main-list oBorder">
					<input class="main-input" v-model="passData" type="text" maxlength="32" placeholder="请输入登录密码" :password="!showPassword" />
					<image :class="['img',showPassword?'cuIcon-attention':'cuIcon-attentionforbid']" @tap="isShowPass"></image>
				</view>
				<view class="main-list oBorder">
					<input class="main-input" v-model="verCode" type="number" maxlength="4" placeholder="请输入验证码" />
					<view :class="['vercode',{'vercode-run': second>0}]" @tap="getVerCode()">{{ getVerCodeSecond }}</view>
				</view>

			</view>

			<button :class="['buttonBorder',!isRotate?'dlbutton':'dlbutton_loading']" @tap="startReg()">
				<view :class="isRotate?'rotate_loop':''">
					<text v-if="isRotate" class="cuIcon-loading1 "></text>
					<text v-if="!isRotate">注 册</text>
				</view>
			</button>

			<!-- 底部信息 -->
			<!-- 			<view class="footer">
				<text 
					@tap="isShowAgree" 
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				>同意</text>
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view> -->
		</view>
	</view>
</template>

<script>
	var _this, countDown;

	export default {
		data() {
			return {
				//logo图片 base64
				phoneData: '', // 用户/电话
				passData: '', //密码
				showPassword: false, //密码是否显示
				second: 0, //倒计时
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		mounted() {
			_this = this;
			clearInterval(countDown); //先清理一次循环，避免缓存
		},
		methods: {

			isShowPass() {
				//是否显示密码
				_this.showPassword = !_this.showPassword
			},
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode() {
				//获取验证码
				if (_this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (_this.second > 0) {
					return false;
				}

				_this._get('user/snsverification', {
						phoneData: this.phoneData, // 用户/电话
					},
					function(result) {
						uni.showModal({
							title: '验证码',
							content: '您的验证码为：' + result.data.snsCode,
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
						_this.second = 60;
						countDown = setInterval(function() {
							_this.second--;
							if (_this.second == 0) {
								clearInterval(countDown)
							}
						}, 1000)
					});
			},
			startReg() {
				_this = this;
				//注册
				if (_this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				// if (_this.showAgree == false) {
				//     uni.showToast({
				//         icon: 'none',
				// 		position: 'bottom',
				//         title: '请先同意《软件用户协议》'
				//     });
				//     return false;
				// }
				if (_this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (_this.passData.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不少于6位'
					});
					return false;
				}
				if (_this.verCode.length != 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}
				
				_this.isRotate = true
				_this._post_form('user/register', {
					phoneData: _this.phoneData, // 用户/电话
					passData: _this.passData, //密码
					verCode: _this.verCode, //验证码
				}, function(result) {
					if (result.data.state === 1) {
						_this.showError(result.data.text);
					}
					if (result.data.state === 0) {
						_this.showError(result.data.text);
						setTimeout(function(){
							_this.isRotate = false
							_this.navigateBack();
						},1200)
					}
					
					_this.isRotate = false
					return false;
				}, false, function() {
					uni.hideLoading();
				});


			},
			/**
			 * 授权成功 跳转回原页面
			 */
			navigateBack: function() {
				uni.reLaunch({
					url: '/pages/login/login',
				});
			}

		},
		computed: {
			getVerCodeSecond() {
				//验证码倒计时计算
				if (this.second <= 0) {
					return '获取验证码';
				} else {
					if (this.second < 10) {
						return '0' + this.second;
					} else {
						return this.second;
					}
				}

			}
		},
	}
</script>

<style>
	@import url("./css/main.css");
</style>
